<!-- 算子库管理页 -->
<template>
  <div class="operator-library">
    <!-- 查询表单 -->
    <el-form class="demo-form-inline" ref="formRef" :model="queryForm" label-width="100px" size="small" inline>
      <el-form-item label="算子名称" prop="name">
        <el-input v-model="queryForm.name" placeholder="算子名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="算子类型" prop="type">
        <el-select v-model="queryForm.type" placeholder="请选择算子类型" clearable>
          <el-option v-for="type in operatorTypes" :key="type.value" :label="type.label" :value="type.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchOperators">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh" @click="resetData">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <div class="el-row">
      <el-button type="success" icon="el-icon-plus" plain size="small" @click="handleAdd">新增算子</el-button>
      <el-button type="danger" icon="el-icon-delete" plain size="small" @click="handleBatchDelete" :disabled="selectedRows.length === 0">批量删除</el-button>
    </div>

    <!-- 算子列表 -->
    <el-table
      :data="tableData"
      height="100%"
      style="width: 100%; "
      @selection-change="handleSelectionChange"
      v-loading="loading"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="序号" width="50"></el-table-column>
      <el-table-column prop="name" label="算子名称"></el-table-column>
      <el-table-column prop="type" label="算子类型">
        <template slot-scope="scope">
          {{ getLabelByValue(operatorTypes, scope.row.type) }}
        </template>
      </el-table-column>
      <el-table-column prop="processingMethod" label="处理方式">
        <template slot-scope="scope">
          {{ getLabelByValue(processingMethods, scope.row.processingMethod) }}
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column label="操作" width="240" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleCopy(scope.row)">复制</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      style="text-align: right"
    ></el-pagination>

    <!-- 新增/编辑算子对话框 -->
    <OperatorEditor
      :visible.sync="dialogVisible"
      :dialogTitle="dialogTitle"
      :formData="form"
      :operatorTypes="operatorTypes"
      :processingMethods="processingMethods"
      @submit="handleSubmit"
    />

    <!-- 算子详情对话框 -->
    <OperatorDetail
      :visible.sync="detailVisible"
      :formData="selectedRow"
      :operatorTypes="operatorTypes"
      :processingMethods="processingMethods"
    />
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import OperatorEditor from './components/OperatorEditor.vue';
import OperatorDetail from './components/OperatorDetail.vue';

export default {
  name: "OperatorLibrary",
  components: { OperatorEditor, OperatorDetail },
  data() {
    return {
      queryForm: {
        name: "",
        type: ""
      },
      currentPage: 1,
      pageSize: 10,
      dialogVisible: false,
      dialogTitle: "",
      form: {
        name: '',
        type: '',
        processingMethod: '',
        description: '',
        content: '',
        processingResult: ''
      },
      selectedRows: [],
      loading: false,
      detailVisible: false,
      selectedRow: {},
      operatorTypes: [
        { value: '基础组件', label: '基础组件' },
        { value: '通用算子', label: '通用算子' },
        { value: '自定义算子', label: '自定义算子' }
      ],
      processingMethods: [
        { value: 'Script 脚本', label: 'Script 脚本' },
        { value: 'RegX 正则表达式', label: 'RegX 正则表达式' },
        { value: 'JSON', label: 'JSON' },
        { value: 'K-V 键值对', label: 'K-V 键值对' }
      ]
    };
  },
  computed: {
    ...mapState('operators', ['operators']), // 从Vuex中获取算子列表
    getLabelByValue() {
      return (options, value) => {
        const item = options.find(item => item.value === value);
        return item ? item.label : '';
      };
    },
    tableData() {
      let filteredData = this.operators;

      if (this.queryForm.name) {
        filteredData = filteredData.filter(item => item.name.includes(this.queryForm.name));
      }

      if (this.queryForm.type) {
        filteredData = filteredData.filter(item => item.type === this.queryForm.type);
      }

      // this.total = filteredData.length;
      return filteredData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    },
    total() {
      let filteredData = this.operators;

      if (this.queryForm.name) {
        filteredData = filteredData.filter(item => item.name.includes(this.queryForm.name));
      }

      if (this.queryForm.type) {
        filteredData = filteredData.filter(item => item.type === this.queryForm.type);
      }

      return filteredData.length;
    }
  },
  methods: {
    ...mapActions('operators', ['fetchOperators', 'addOperator', 'updateOperator', 'deleteOperator']),

    fetchOperators() {
      this.loading = true;
      this.$store.dispatch('operators/fetchOperators').then(() => {
        this.loading = false;
      });
    },
    resetData() {
      this.$refs.formRef.resetFields();
      this.fetchOperators();
    },
    handleAdd() {
      this.dialogTitle = "新增算子";
      this.form = {
        name: '',
        type: '',
        processingMethod: '',
        description: '',
        content: '',
        processingResult: ''
      };
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.dialogTitle = "编辑算子";
      this.form = { ...row };
      this.dialogVisible = true;
    },
    handleCopy(row) {
      const newOperator = { ...row };
      newOperator.id = this.operators.length ? this.operators[this.operators.length - 1].id + 1 : 1;
      newOperator.name += '_copy';
      this.addOperator(newOperator);
      this.fetchOperators();
    },
    handleDetail(row) {
      this.selectedRow = row;
      this.detailVisible = true;
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该算子信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOperator(row.id);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的算子信息!'
        });
        return;
      }
      this.$confirm('此操作将永久删除选中的算子信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.selectedRows.forEach(row => this.deleteOperator(row.id));
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchOperators();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchOperators();
    },
    handleSubmit(formData) {
      if (this.dialogTitle === "新增算子") {
        formData.id = this.operators.length ? this.operators[this.operators.length - 1].id + 1 : 1;
        this.addOperator(formData);
      } else {
        this.updateOperator(formData);
      }
      this.dialogVisible = false;
    }
  },
  mounted() {
    this.fetchOperators();
  }
};
</script>

<style scoped lang="scss">
.operator-library {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;

  .el-row {
    margin-bottom: 20px;
  }

  .el-table {
    flex: 1;
    margin-bottom: 20px;
  }
}
</style>
